
<h1><span class="yiyi-st" id="yiyi-12">numpy.nansum</span></h1>
        <blockquote>
        <p>原文：<a href="https://docs.scipy.org/doc/numpy/reference/generated/numpy.nansum.html">https://docs.scipy.org/doc/numpy/reference/generated/numpy.nansum.html</a></p>
        <p>译者：<a href="https://github.com/wizardforcel">飞龙</a> <a href="http://usyiyi.cn/">UsyiyiCN</a></p>
        <p>校对：（虚位以待）</p>
        </blockquote>
    
<dl class="function">
<dt id="numpy.nansum"><span class="yiyi-st" id="yiyi-13"> <code class="descclassname">numpy.</code><code class="descname">nansum</code><span class="sig-paren">(</span><em>a</em>, <em>axis=None</em>, <em>dtype=None</em>, <em>out=None</em>, <em>keepdims=&lt;class numpy._globals._NoValue&gt;</em><span class="sig-paren">)</span><a class="reference external" href="http://github.com/numpy/numpy/blob/v1.11.3/numpy/lib/nanfunctions.py#L448-L536"><span class="viewcode-link">[source]</span></a></span></dt>
<dd><p><span class="yiyi-st" id="yiyi-14">返回在给定轴上处理非数字（NaN）为零的数组元素的总和。</span></p>
<p><span class="yiyi-st" id="yiyi-15">在Numpy版本</span><span class="yiyi-st" id="yiyi-16">在以后的版本中返回零。</span></p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name">
<col class="field-body">
<tbody valign="top">
<tr class="field-odd field"><th class="field-name"><span class="yiyi-st" id="yiyi-17">参数：</span></th><td class="field-body"><p class="first"><span class="yiyi-st" id="yiyi-18"><strong>a</strong>：array_like</span></p>
<blockquote>
<div><p><span class="yiyi-st" id="yiyi-19">数组包含需要和的数字。</span><span class="yiyi-st" id="yiyi-20">如果<em class="xref py py-obj">a</em>不是数组，则尝试进行转换。</span></p>
</div></blockquote>
<p><span class="yiyi-st" id="yiyi-21"><strong>axis</strong>：int，可选</span></p>
<blockquote>
<div><p><span class="yiyi-st" id="yiyi-22">计算和的轴。</span><span class="yiyi-st" id="yiyi-23">默认值是计算展平的数组的总和。</span></p>
</div></blockquote>
<p><span class="yiyi-st" id="yiyi-24"><strong>dtype</strong>：数据类型，可选</span></p>
<blockquote>
<div><p><span class="yiyi-st" id="yiyi-25">返回的数组和累加器元素的累加器的类型。</span><span class="yiyi-st" id="yiyi-26">默认情况下，使用<em class="xref py py-obj">a</em>的dtype。</span><span class="yiyi-st" id="yiyi-27">一个例外是当<em class="xref py py-obj">a</em>具有比平台（u）intp精度更低的整数类型时。</span><span class="yiyi-st" id="yiyi-28">在这种情况下，默认值将是（u）int32或（u）int64，具体取决于平台是32位还是64位。</span><span class="yiyi-st" id="yiyi-29">对于不精确的输入，dtype必须不精确。</span></p>
<div class="versionadded">
<p><span class="yiyi-st" id="yiyi-30"><span class="versionmodified">版本1.8.0中的新功能。</span></span></p>
</div>
</div></blockquote>
<p><span class="yiyi-st" id="yiyi-31"><strong>out</strong>：ndarray，可选</span></p>
<blockquote>
<div><p><span class="yiyi-st" id="yiyi-32">备用输出放置结果的数组。</span><span class="yiyi-st" id="yiyi-33">默认值为<code class="docutils literal"><span class="pre">None</span></code>。</span><span class="yiyi-st" id="yiyi-34">如果提供，它必须具有与预期输出相同的形状，但如果必要，将转换类型。</span><span class="yiyi-st" id="yiyi-35">有关详细信息，请参阅<code class="xref py py-obj docutils literal"><span class="pre">doc.ufuncs</span></code>。</span><span class="yiyi-st" id="yiyi-36">将NaN转换为整数可能会产生意想不到的结果。</span></p>
<div class="versionadded">
<p><span class="yiyi-st" id="yiyi-37"><span class="versionmodified">版本1.8.0中的新功能。</span></span></p>
</div>
</div></blockquote>
<p><span class="yiyi-st" id="yiyi-38"><strong>keepdims</strong>：bool，可选</span></p>
<blockquote>
<div><p><span class="yiyi-st" id="yiyi-39">如果设置为True，则缩小的轴在结果中保留为尺寸为1的尺寸。</span><span class="yiyi-st" id="yiyi-40">使用此选项，结果将与原始<em class="xref py py-obj">a</em>正确地广播。</span></p>
<p><span class="yiyi-st" id="yiyi-41">If the value is anything but the default, then <em class="xref py py-obj">keepdims</em> will be passed through to the <a class="reference internal" href="numpy.mean.html#numpy.mean" title="numpy.mean"><code class="xref py py-obj docutils literal"><span class="pre">mean</span></code></a> or <a class="reference internal" href="numpy.sum.html#numpy.sum" title="numpy.sum"><code class="xref py py-obj docutils literal"><span class="pre">sum</span></code></a> methods of sub-classes of <a class="reference internal" href="numpy.ndarray.html#numpy.ndarray" title="numpy.ndarray"><code class="xref py py-obj docutils literal"><span class="pre">ndarray</span></code></a>. </span><span class="yiyi-st" id="yiyi-42">如果子类方法不实现<em class="xref py py-obj">keepdims</em>，则会引发任何异常。</span></p>
<div class="versionadded">
<p><span class="yiyi-st" id="yiyi-43"><span class="versionmodified">版本1.8.0中的新功能。</span></span></p>
</div>
</div></blockquote>
</td>
</tr>
<tr class="field-even field"><th class="field-name"><span class="yiyi-st" id="yiyi-44">返回：</span></th><td class="field-body"><p class="first last"><span class="yiyi-st" id="yiyi-45"><strong>y</strong>：ndarray或numpy scalar</span></p>
</td>
</tr>
</tbody>
</table>
<div class="admonition seealso">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-46">也可以看看</span></p>
<dl class="last docutils">
<dt><span class="yiyi-st" id="yiyi-47"><a class="reference internal" href="numpy.sum.html#numpy.sum" title="numpy.sum"><code class="xref py py-obj docutils literal"><span class="pre">numpy.sum</span></code></a></span></dt>
<dd><span class="yiyi-st" id="yiyi-48">数组传播NaNs。</span></dd>
<dt><span class="yiyi-st" id="yiyi-49"><a class="reference internal" href="numpy.isnan.html#numpy.isnan" title="numpy.isnan"><code class="xref py py-obj docutils literal"><span class="pre">isnan</span></code></a></span></dt>
<dd><span class="yiyi-st" id="yiyi-50">显示哪些元素是NaN。</span></dd>
<dt><span class="yiyi-st" id="yiyi-51"><a class="reference internal" href="numpy.isfinite.html#numpy.isfinite" title="numpy.isfinite"><code class="xref py py-obj docutils literal"><span class="pre">isfinite</span></code></a></span></dt>
<dd><span class="yiyi-st" id="yiyi-52">显示哪些元素不是NaN或+/- inf。</span></dd>
</dl>
</div>
<p class="rubric"><span class="yiyi-st" id="yiyi-53">笔记</span></p>
<p><span class="yiyi-st" id="yiyi-54">如果存在正和负无穷大，则和将是非数字（NaN）。</span></p>
<p><span class="yiyi-st" id="yiyi-55">Numpy整数运算是模块化的。</span><span class="yiyi-st" id="yiyi-56">如果和的大小超过整数累加器的大小，其值将回绕，结果将不正确。</span><span class="yiyi-st" id="yiyi-57">指定<code class="docutils literal"><span class="pre">dtype=double</span></code>可以缓解这个问题。</span></p>
<p class="rubric"><span class="yiyi-st" id="yiyi-58">例子</span></p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">nansum</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="go">1</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">nansum</span><span class="p">([</span><span class="mi">1</span><span class="p">])</span>
<span class="go">1</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">nansum</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">nan</span><span class="p">])</span>
<span class="go">1.0</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">],</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">nan</span><span class="p">]])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">nansum</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
<span class="go">3.0</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">nansum</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
<span class="go">array([ 2.,  1.])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">nansum</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">nan</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">inf</span><span class="p">])</span>
<span class="go">inf</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">nansum</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">nan</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">NINF</span><span class="p">])</span>
<span class="go">-inf</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">nansum</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">nan</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">inf</span><span class="p">,</span> <span class="o">-</span><span class="n">np</span><span class="o">.</span><span class="n">inf</span><span class="p">])</span> <span class="c1"># both +/- infinity present</span>
<span class="go">nan</span>
</pre></div>
</div>
</dd></dl>
